<?php
class SearchController extends AppController{
    public function index()
    {
      
      if(isset($_GET['key_word']))
      {
        $key_word = $_GET['key_word'];
        $array_key_word = explode('+', $key_word);
        $search = "";
        $i = 1;
        foreach($array_key_word as $key_word)
        {
            if($search == '')
            {
                $search = " AND product_desc.title LIKE '%$key_word%' OR product_desc.description LIKE '%$key_word%' ";
            }
            else
            {
                $search .= " OR product_desc.title LIKE '%$key_word%' OR product_desc.description LIKE '%$key_word%' ";
            }
        }
        $lang_code = $this->Session->read('lang_code');
        $sql = "SELECT *
                FROM product_desc, product
                LEFT JOIN link ON product.product_id = link.post_id AND link.lang_code = '$lang_code' AND link.type = 'product'
                LEFT JOIN image ON product.product_id = image.post_id AND image.type = 'product' AND image.`status` = 1
                WHERE product.product_id = product_desc.product_id AND product_desc.lang_code = '$lang_code'
                AND product.`status` = 1 AND (1=1 $search)";
        $this->loadModel('Product'); $this->Product->lang_code = $lang_code;
        $result_search = $this->Product->query($sql);
        $this->set('result_search', $result_search);

        $products_favorite = $this->Product->get_product_favorite('cutkit');
        $this->set('products_favorite', $products_favorite);
        
        $breadcrumbs[] = array('title' => 'Tìm kiếm', 'link' => 'search?key_word='.$key_word);
        $this->set('breadcrumbs', $breadcrumbs);
      }
      else
      {
        return $this->redirect(array('controller' => 'intro', 'action' => 'index'));
      }
      
      
        $meta_title = 'Tìm kiếm';
        $this->set('meta_title', $meta_title);
        
    }

}